package com.fjp.heartguard.app.weight.seekbar

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import androidx.compose.ui.unit.sp
import com.fjp.heartguard.app.ext.dp
import me.hgj.jetpackmvvm.ext.util.dp2px

class HTSeekBar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {
    private var labs: List<HTSeekBarLab> = ArrayList()
    private var thumbValue: Int = 0
    private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val barPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val whiteLinePaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val thumbPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val thumbOutLinePaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val barHeight = dp2px(16).toFloat()

    private var backgroundRect: RectF? = null
    private val progressRects: MutableList<RectF> = mutableListOf()

    init {
        init(context, attrs)
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        textPaint.style = Paint.Style.FILL
        textPaint.textSize = dp2px(12).toFloat()
        textPaint.textAlign = Paint.Align.CENTER
        barPaint.style = Paint.Style.FILL
        barPaint.strokeWidth = barHeight
        whiteLinePaint.strokeWidth =dp2px(2).toFloat()
        whiteLinePaint.style = Paint.Style.FILL_AND_STROKE
        whiteLinePaint.color = Color.WHITE

        thumbPaint.strokeWidth = barHeight / 2
        thumbPaint.isAntiAlias = true
        thumbPaint.style = Paint.Style.STROKE
        thumbPaint.color = Color.BLUE

        thumbOutLinePaint.strokeWidth = barHeight / 2
        thumbOutLinePaint.isAntiAlias = true
        thumbOutLinePaint.style = Paint.Style.STROKE
        thumbOutLinePaint.color = Color.WHITE
        thumbOutLinePaint.setShadowLayer(2f, 1f, 1f, Color.GRAY)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        backgroundRect = RectF(0f, 0f, w.toFloat(), h.toFloat())
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        progressRects.clear()
        if (labs.size == 0) {
            return
        }
        val maxValue = labs[labs.size - 1].value
        for (i in 0 until labs.size) {
            var rightf = 0.0f
            var leftf = 0.0f
            if (i != 0) {
                leftf = progressRects[i - 1].right
            }
            rightf = labs[i].value * 1.0f / maxValue * backgroundRect!!.right
            val rectf = RectF(leftf, dp2px(15).toFloat(), rightf, barHeight + dp2px(15).toFloat())
            progressRects.add(rectf)
            barPaint.color = Color.parseColor(this.labs[i].color)
            //画seekbar
            canvas.drawRect(rectf, barPaint)
        }
        for ((index, rectF) in progressRects.withIndex()) {
            //画白条分割
            if (index != progressRects.size - 1) {
                canvas.drawRect(RectF(rectF.right, rectF.top, rectF.right, rectF.bottom), whiteLinePaint)
            }
            //画文字
            textPaint.color = Color.parseColor(labs[index].color)
            val text = labs[index].des
            val width = textPaint.measureText(text)
            canvas.drawText(text, rectF.left + (rectF.right - rectF.left) / 2, backgroundRect!!.top + barHeight+dp2px(36).toFloat(), textPaint)
        }

        val potinX = thumbValue * 1f / maxValue * width
        val lab = labs.find { it.value >= thumbValue }
        thumbPaint.color = Color.parseColor(lab!!.color)

        val OutLinePath = Path();
        OutLinePath.moveTo(potinX - dp2px(10).toFloat(), 0f)
        OutLinePath.lineTo(potinX + dp2px(10).toFloat(), 0f)
        OutLinePath.lineTo(potinX * 1f, barHeight)
        OutLinePath.close()
        canvas.drawPath(OutLinePath, thumbOutLinePaint)
        val path = Path();
        path.moveTo(potinX - dp2px(7).toFloat(), 0f)
        path.lineTo(potinX + dp2px(7).toFloat(), 0f)
        path.lineTo(potinX * 1f, barHeight-dp2px(3).toFloat())
        path.close()
        canvas.drawPath(path, thumbPaint)

    }

    fun setLabs(labs: ArrayList<HTSeekBarLab>, thumbValue: Int) {
        this.labs = labs
        this.thumbValue = thumbValue
        invalidate()
    }

}
